<template>
  <div class="corse-video">
    <el-card>
      <div slot="header">
        <div>课程：</div>
        <div>阶段：</div>
        <div>课时：</div>
      </div>
      <el-form label-width="40">
        <el-form-item label="视频">
          <input type="file" ref="video-file" />
        </el-form-item>
        <el-form-item label="封面">
          <input type="file" ref="image-file" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleUpload">开始上传</el-button>
          <el-button>返回</el-button>
        </el-form-item>
        <el-form-item>
          <p>视频上传中：{{ uploadPercent }}%</p>
          <p v-if="isUploadSucess">
            视频转码中：{{ isTransCodeSuccess ? "完成" : "..." }}
          </p>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { uploadCourseImage } from '@/services/course'
import Vue from 'vue'
import {
  aliyunVideoUploadAddressAdnAuth,
  aliyunImagUploadAddressAdnAuth,
  getAliyunTransCodePercent,
  transCodeVideo
} from '@/services/aliyun-upload'

export default Vue.extend({
  name: 'CourseVideo',
  data () {
    return {
      uploader: null,
      imageUrl: '',
      videoId: '',
      uploadPercent: 0,
      isUploadSucess: false,
      isTransCodeSuccess: false
    }
  },
  computed: {
    video () {
      return this.$refs['video-file']
    },
    image () {
      return this.$refs['image-file']
    }
  },
  created () {
    this.initUploader()
  },
  methods: {
    initUploader () {
      /* eslint-disable */
      const self = this; // eslint-disable-line
      this.uploader = new window.AliyunUpload.Vod({
        //阿里账号ID，必须有值
        userId: "1618139964448548",
        //上传到视频点播的地域，默认值为'cn-shanghai'，//eu-central-1，ap-southeast-1
        region: "",
        //分片大小默认1 MB，不能小于100 KB
        partSize: 1048576,
        //并行上传分片个数，默认5
        parallel: 5,
        //网络原因失败时，重新上传次数，默认为3
        retryCount: 3,
        //网络原因失败时，重新上传间隔时间，默认为2秒
        retryDuration: 2,
        //开始上传
        onUploadstarted: async function (uploadInfo: any) {
          let uploadAddressAndauth = null;
          if (uploadInfo.isImage) {
            const { data } = await aliyunImagUploadAddressAdnAuth();
            self.imageUrl = data.data.imageUrl;
            uploadAddressAndauth = data.data;
          } else {
            const { data } = await aliyunVideoUploadAddressAdnAuth({
              fileName: uploadInfo.file.name,
              imageUrl: self.imageUrl,
            });
            uploadAddressAndauth = data.data;
            self.videoId = uploadAddressAndauth.videoId;
          }

          (self.uploader as any).setUploadAuthAndAddress(
            uploadInfo,
            uploadAddressAndauth.uploadAuth,
            uploadAddressAndauth.uploadAddress,
            uploadAddressAndauth.imageId || uploadAddressAndauth.videoId
          );
          // setUploadAuthAndAddress(uploadFileInfo, uploadAuth, uploadAddress,videoId)
        },
        //文件上传成功
        onUploadSucceed: function (uploadInfo: any) {
          //
        },
        //文件上传失败
        onUploadFailed: function (uploadInfo: any, code: any, message: any) {
          //
        },
        //文件上传进度，单位：字节
        onUploadProgress: function (
          uploadInfo: any,
          totalSize: any,
          loadedPercent: any
        ) {
          //
          if (!uploadInfo.isImage) {
            this.uploadPercent = Math.floor(loadedPercent * 100);
          }
        },
        //上传凭证超时
        onUploadTokenExpired: function (uploadInfo: any) {
          //
        },
        //全部文件上传结束
        onUploadEnd: async function (uploadInfo: any) {
          // 上传完成 转码
          this.isUploadSucess = true;
          const { data } = await transCodeVideo({
            lessonId: self.$route.query.lessonId,
            coverImageUrl: this.imageURL,
            fileName: (self.video as any).files[0].name,
            fileId: self.videoId,
          });
          const timer = setInterval(async () => {
            const { data } = await getAliyunTransCodePercent(
              self.$route.query.lessonId
            );
            if (data.data === 100) {
              self.isTransCodeSuccess = true;
              window.clearInterval(timer);
              self.$message.success("转码成功");
            }
          }, 3000);
        },
      });
      /* eslint-enable */
    },
    handleUpload () {
      this.uploadPercent = 0
      this.isUploadSucess = false
      this.isTransCodeSuccess = false
      const videoFile = (this.video as any).files[0]
      const imageFile = (this.image as any).files[0]
      const uploader = this.uploader as any

      uploader.addFile(imageFile, null, null, null, '{"Vod": {}}')
      uploader.addFile(videoFile, null, null, null, '{"Vod": {}}')

      uploader.startUpload()
    }
  }
})
</script>
<style lang="scss" scoped>
</style>
